// pages/ScanTask/ScanTask.js
const httpRequest = require('../../utils/http_utils.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '维保任务',
    Code: '', //设备编码
    info: '', //提示消息
    listInfo: [], //数据集合
    totalNum: 0, //数据总数
    scrollHeight: 0, //滚动条高度
    Refresh:false, //是否刷新
  },

  //初始化数据
  initData: function() {
    var that = this;
    var param = {
      RowCode: that.data.Code
    }
    httpRequest.GetValidateRequest(httpRequest.apiInfo.GetMaintenanceScanApi, param,
      function(data) {
        if (data.Totalnum == 0) {
          that.setData({
            info: '当前设备没有任务！'
          })
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2];  //上一个页面

          prevPage.setData({
            Refresh: true
          });
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        } else if (data.Totalnum == 1) {
          for (var i = 0; i < data.Data.length; i++) {
            wx.redirectTo({
              url: '../MaintainAdd/MaintainAdd?equipmentname=' + data.Data[i].EquipmentName + '&taskbegintime=' + data.Data[i].TaskBeginTime + '&taskendtime=' + data.Data[i].TaskEndTime + '&equipmentlocation=' + data.Data[i].EquipmentLocation + '&taskguid=' + data.Data[i].RowGuid
            })
          }
        } else {
          var listArray = that.data.listInfo
          for (var i = 0; i < data.Data.length; i++) {
            listArray.push(data.Data[i])
          }
          that.setData({
            listInfo: listArray,
            totalNum: data.Totalnum
          })
        }
      },
      function() {})
  },

  //页面跳转
  tiaozhuan: function(e) {
    wx.navigateTo({
      url: '../MaintainAdd/MaintainAdd?taskguid=' + e.currentTarget.dataset.taskguid + '&equipmentname=' + e.currentTarget.dataset.equipmentname + '&taskbegintime=' + e.currentTarget.dataset.taskbegintime + '&taskendtime=' + e.currentTarget.dataset.taskendtime + '&equipmentlocation=' + e.currentTarget.dataset.equipmentlocation,
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      Code: options.code,
      scrollHeight: (app.globalData.titleBarHeight + app.globalData.statusBarHeight) * 2
    })
    this.initData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.Refresh == true) {
      this.setData({
        listInfo: []
      })
      this.initData();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})